<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-distLabel-edit" th:object="${distLabel}">
            <input id="id" name="id" th:field="*{id}"  type="hidden">
            <div class="form-group">	
                <label class="col-sm-3 control-label">分配批次：</label>
                <div class="col-sm-8">
                    <input id="distNo" name="distNo" th:field="*{distNo}" class="form-control" type="text" readonly="readonly">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">厂商：</label>
                <div class="col-sm-8">
                   <select id="gardenNo" name="gardenNo"  class="form-control m-b" th:with="type=${@garden.getGarden()}">
                   		<option value="">---请选择---</option>
	                    <option th:each="garden : ${type}" th:text="${garden.gardenName+garden.teaGardenNo}" th:value="${garden.teaGardenNo}" th:field="*{gardenNo}"></option>
	                </select>
                </div>
            </div>
             <div class="form-group">	
                <label class="col-sm-3 control-label">入库批次号：</label>
                <div class="col-sm-8">
                    <input id="depotHeadNo" name="depotHeadNo" th:field="*{depotHeadNo}" class="form-control" type="text" readonly="readonly">
                </div>
            </div>
             <div class="form-group">	
                <label class="col-sm-3 control-label">起始序列号：</label>
                <div class="col-sm-8">
                    <input id="beginSerialNumber" name="beginSerialNumber" th:field="*{beginSerialNumber}" class="form-control" type="text" readonly="readonly">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">分配数量：</label>
                <div class="col-sm-8">
                    <input id="distNum" name="distNum" th:field="*{distNum}" class="form-control" type="text" readonly="readonly">
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">标签信息：</label>
                <div class="col-sm-8">
                	<input id="bindingTagNo1" name="bindingTagNo1" th:field="*{bindingTagNo}" class="form-control" type="hidden"> 
                   <select id=bindingTagNo name="bindingTagNo"  class="form-control m-b" disabled="disabled">
	                    <option value="">---请选择---</option>
	                </select>
                </div>
            </div>
            <div class="form-group">	
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input id="remark" name="remark" th:field="*{remark}" class="form-control" type="text">
                </div>
            </div>
		</form>
		<!-- <div class="col-sm-3" id="tips" align="left"></div> -->
		<span class="col-sm-8" id="tips" style="color:red; margin-left: 190px"></span>
    </div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
		var prefix = ctx + "module/distLabel"
		$(function(){
			  getReleaseStatus();
			   $.ajax({ 
			    	url:ctx +'module/tagInfo/getTagInfo', 
			    	type:"post",
			    	dataType:"json", 
			    	async : false,
			    	data: {teaGardenNo : $("#gardenNo").val()}, 
			    	success: function (data) { 
			    		  for(var i = 0, len = data.length; i < len; i++) { 
			    			$("#bindingTagNo").append($("<option value='" + data[i].id + "'>" + data[i].tagInfo + "</option>")); 
			    			}  
			    		} , 
			    	error : function(request) {
	      						$.modal.alertError("系统错误");
	      				},
			    	});
			   if($("#bindingTagNo1").val() !=""){
			   			$("#bindingTagNo").find("option[value="+$("#bindingTagNo1").val()+"]").attr("selected",true); 
			   }
		})
		$("#form-distLabel-edit").validate({
			rules:{
				distNo:{
					required:true,
				},
				gardenNo:{
					required:true,
				},
				depotHeadNo:{
					required:true,
				},
				beginSerialNumber:{
					required:true,
				},
				distNum:{
					required:true,
					isDigits:true,
				},
				bindingTagNo:{
					required:true,
				},
				remark:{
					maxlength: 255
				},
			}
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	console.log($("#bindingTagNo").val());
	        	//return;
	            /* $.operate.save(prefix + "/edit", $('#form-distLabel-edit').serialize()); */
	            editSave();
	        }
	    }
		$("#gardenNo").change(function(){
			 $("#bindingTagNo").removeAttr("disabled");
			 var  gardenNo = $("#gardenNo").find("option:selected").val(); 
			 if(gardenNo == "") { 
				 $("#bindingTagNo").empty().append("<option value=''>---请选择---</option>"); 
				 $("#bindingTagNo").attr("disabled","disabled");
				 return false;
				 }
		    $("#bindingTagNo").empty().append("<option value=''>---请选择---</option>");
		     $.ajax({ 
		    	url:ctx +'module/tagInfo/getTagInfo', 
		    	type:"post",
		    	dataType:"json", 
		    	data: {teaGardenNo : $(this).val()}, 
		    	success: function (data) { 
		    		  for(var i = 0, len = data.length; i < len; i++) { 
		    			$("#bindingTagNo").append($("<option value='" + data[i].id + "'>" + data[i].tagInfo + "</option>")); 
		    			}  
		    		} , 
		    	error : function(request) {
      						$.modal.alertError("系统错误");
      				},
		    	}); 
		    	
			});
		
		function editSave(){
		 	$.ajax({
        		cache : true,
        		type : "POST",
        		url : prefix + "/edit",
        		data : {
        			"id" :  $("#id").val(),
        			"distNo" : $("#distNo").val(),
        			"gardenNo" : $("#gardenNo").val(),
        			"depotHeadNo" : $("#depotHeadNo").val(),
        			"beginSerialNumber" : $("#beginSerialNumber").val(),
        			"distNum" : $("#distNum").val(),
        			"bindingTagNo" : $("#bindingTagNo").val(),
        			"remark" : $("#remark").val(),
        		},
        		async : false,
        		error : function(request) {
        			$.modal.alertError("系统错误");
        		},
        		success : function(data) {
        			$.operate.saveSuccess(data);
        		}
        	});
		}
		
		function getReleaseStatus(){
			   $.ajax({ 
			    	url:ctx +'module/depotHeadItem/getReleaseStatus', 
			    	type:"post",
			    	dataType:"json", 
			    	async : false,
			    	data: {depotHeadNo : $("#depotHeadNo").val()}, 
			    	success: function (data) { 
			    			if(data){
			    				$("#gardenNo").attr("disabled","disabled");
			    				$("#bindingTagNo").attr("disabled","disabled");
			    				 $("#tips").html("提示信息：该入库批次下存在已发行的标签，除备注以外的信息不可修改。");
			    			}else{
			    				$("#bindingTagNo").removeAttr("disabled");
			    			}
			    		} , 
			    	error : function(request) {
	      						$.modal.alertError("系统错误");
	      				},
			    	});
		}
	</script>
</body>
</html>
